package com.tianma.config;

import com.alibaba.otter.canal.client.CanalConnector;
import com.alibaba.otter.canal.client.CanalConnectors;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.InetSocketAddress;

@Configuration
public class CanalConfig {
    @Value("${canal.server.ip}")
    private String canalIp;

    @Value("${canal.server.port}")
    private Integer canalPort;

    @Value("${canal.destination}")
    private String destination;

    @Value("${zookeeper.server.ip}")
    private String zkServerIp;

    @Value("${canal.user}")
    private String canalUser;

    @Value("${canal.passwd}")
    private String canalPasswd;

    /**
     * 获取简单 canal-server 连接
     * @return
     */
    @Bean
    public CanalConnector canalSimpleConnector() {
        CanalConnector canalConnector = CanalConnectors.newSingleConnector(new InetSocketAddress(canalIp, canalPort), destination, canalUser, canalPasswd);
        return canalConnector;
    }

    /**
     * 通过连接 zookeeper 获取 canal-server 连接
     * @return
     */
//    @Bean
    public CanalConnector canalHaConnector() {
        CanalConnector canalConnector = CanalConnectors.newClusterConnector(zkServerIp, destination, canalUser, canalPasswd);
        return canalConnector;
    }
}
